<?php
    @include_once("./conn.php");


    // 无脑新增 => 添加重复数据 => 加入之前需要判断 当前用户是否购买过该商品
    // 没买过  => 直接新增 
    // 买过    => 数量累加(更新)

    // 当前用户是否购买过该商品
    $search = "select * from `shoppingcar` where user = 'a123123' and goodsId = '49142034184'";
    $result = mysqli_query($conn,$search);

    $item = mysqli_fetch_assoc($result);  // 查询的结果

    if($item){   // 买过    => 数量累加(更新)
        $sql = "update `shoppingcar` set buyNum = buyNum + 1 where user = 'a123123' and goodsId = '49142034184'";
    }else{   //  没买过  => 直接新增 
         // 无脑新增 => 添加重复数据
        $sql = "insert into `shoppingcar`(user,goodsId,buyNum) value('b123123','49142034184',1)";
    }
    mysqli_query($conn,$sql);   //执行sql

    // 增删改  返回 => 受影响的行数(用不着接收结果对象了).
    // $rows > 0  新增成功
    // $rows = 0  新增没有
    // $rows = -1  新增失败  语句执行有误
    $rows = mysqli_affected_rows($conn);

    $obj = array();
    if($rows>0){
        $obj["status"] = true;
        $obj["detail"] = "添加成功";
    }else{
        $obj["status"] = false;
        $obj["detail"] = "添加失败,sql语句有误";
        $obj["sql"] = $sql;
    }
    echo json_encode($obj);
   

?>